﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.internals.classes
{
    public partial class Edit : TeacherBasePage
    {
        public string act;
        public Dictionary<int, string> projectList = new Dictionary<int, string>();
        public IList<DictAreaModel> areaList;
        public int id;
        public InternalClassModel model = new InternalClassModel();

        protected override void ShowPage()
        {
            if (authority != E.Enum.EnumTeacherAuthority.Admin)
            {
                projectList = (new CourseBLL()).GetTeacherProjectList(teacherid);
            }
            else
            {
                projectList = (new CourseBLL()).GetInternalProjectList();
            }

            act = XRequest.GetString("act");
            id = XRequest.GetInt("id", -1);

            ClassBLL bll = new ClassBLL();

            if (act == "u")
            {
                if (id != -1)
                {
                    model = bll.GetInternalClassInfoById(id);
                    if (model.ID == -1)
                    {
                        Alert("参数错误");
                        return;
                    }
                }
                else
                {
                    Alert("参数错误");
                    return;
                }
            }

            if (ispost)
            {
                if (CheckValue())
                {
                    string classcode = XRequest.GetString("ClassCodeTxt");

                    model = new InternalClassModel();
                    model.ClassCode = classcode;

                    int rst = 0;
                    string href = "";
                    switch (act)
                    {
                        case "n":
                            model.ProjectID = XRequest.GetInt("ProjectSlt", -1);
                            model.CourseID = XRequest.GetInt("CourseSlt", -1);
                            model.CourseArrayId = XRequest.GetString("courseIdArray");
                            model.StudentArrayId = XRequest.GetString("studentIdArray");

                            model.TeacherID = teacherid;
                            model.SignUpType = XRequest.GetInt("radSignUpType", 0);
                            rst = bll.AddInternalClass(model);
                            href = "/internals/classes/Edit.aspx?act=n";
                            break;
                        case "u":
                            model.ID = id;

                            rst = bll.UpdateInternalClass(model);
                            href = "/internals/classes/Edit.aspx?act=u&id=" + id;
                            break;
                        default:
                            break;
                    }

                    if (rst > 0)
                    {
                        ExecuteJS("alert('操作成功'); window.location = '" + href + "';");
                    }
                    else
                    {
                        ExecuteJS("alert('操作失败'); history.back();");
                    }
                }
                else
                {
                    ExecuteJS("alert('信息填写错误'); history.back();");
                }
            }

        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("ClassCodeTxt").Equals(""))
            {
                Alert("班级编号不能为空");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetString("courseIdArray") == "")
            {
                Alert("请选择课程");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetInt("ProjectSlt", -1) == -1)
            {
                Alert("请选择项目");
                rst = false;
            }

            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
